/*******************************************************************
REPLICATION FILE FOR:
Paper: “Class, Ethnicity, and Cooperation Among Women: Evidence from a Public Goods Experiment in Lebanon”
Authors: Leslie Marshall and Laura Paler

This do file performs all analysis and produces all tables and figures in the main paper and supplementary online appendix
********************************************************************/


*******************************************************
*SET GLOBAL DIRECTORIES
*******************************************************

*Set your personal working directory here: 
*Note that computers running Windows need to reverse the dashes from "/" to "\"

	cd ~/

	global dos "DOs_and_ADOs"
	global packages "DOs_and_ADOs/PACKAGES"

	global data_orig "DATA/ORIG"
	global data_newimp "DATA/NEW_IMPUTE"
	global working "DATA/NEW_IMPUTE/WORKING"

	global results "RESULTS" 
	global results_figs "RESULTS/FIGS"
	global results_tables "RESULTS/TABLES"
	
	set more off
	
*Close any open logs and start a new log
	log close
	log using "$results/Leb_PGG_4_Analyze_Log", replace
	

*Calls data prepped in Gender_PGG_varprep.do
	use "$data_orig/Leb_PGG_3_Clean_FINAL.dta", clear

		
***************************************************************************
* MACROS
***************************************************************************

*Main control macros (keep out pre_q3_christian for collinearity)
	global c_main pre_q2_age married scr_q4_educ_REC2 pre_q3_shia pre_q3_sunni index_c_econ_all_wgt4 student homemaker index_c_prej_wgt4 index_c_act_wgt4 index_c_soc_hom_wgt4 pre_q13_disagree_REC1 index_c_sect_id_wgt4 index_connect_wgt4  pre_q14_a_agegrp_REC1b pre_q14_b_gender_REC1b pre_q14_d_class_REC1b pre_q14_leb_REC1b pre_q14_f_occupation_REC1b 
		
	global c_main_noecon pre_q2_age married scr_q4_educ_REC2 pre_q3_shia pre_q3_sunni student homemaker index_c_prej_wgt4 index_c_act_wgt4 index_c_soc_hom_wgt4 pre_q13_disagree_REC1 index_c_sect_id_wgt4 index_connect_wgt4 pre_q14_a_agegrp_REC1b pre_q14_b_gender_REC1b pre_q14_d_class_REC1b pre_q14_leb_REC1b pre_q14_f_occupation_REC1b 
		
*Implementation vars
	global c_implement mod1 n_gid post_q7_knowppl_REC1 days_to_elex

	global c_implement_nosect mod1 n_gid post_q7_knowppl_REC1 days_to_elex


*Public goods payoff comprehension
	global c_pgg_payoff_comp pgg_prac_prob

	global pgg_payoffs_all pgg_prac_prob ew_eachearn_REC2 ew_potshare ew_totalearn_REC2
	
*ALL CONTROLS
	global c_all pre_q2_age married scr_q4_educ_REC2 pre_q3_christian pre_q3_shia pre_q3_sunni index_c_econ_all_wgt4 scr_q9_assets_REC1 scr_q10_hh_area scr_q11_summer_hh scr_q12_poweralt scr_q13_vacay scr_q14_dineout_REC1 scr_q15_income scr_q16_income_subj_REC1 pre_q8_income_REC1 pre_q9_class_REC3b student homemaker index_c_prej_wgt4 pre_q12_a_marry_REC1 pre_q12_b_doctor_REC1 pre_q12_c_neighbor_REC1 pre_q12_d_poltalk_REC1 pre_q12_e_econtalk_REC1 pre_q12_f_supervisor_REC1 pre_q12_g_friends_REC1 index_c_act_wgt4 pre_q7_a_friends_REC1 pre_q7_b_elites_REC1 pre_q7_c_petition_REC1 pre_q7_d_protest_REC1 index_c_soc_hom_wgt4 pre_q10_diffecon_REC1 pre_q5_diffsect_REC1 pre_q13_disagree_REC1 index_c_sect_id_wgt4 pre_q4_changesect_REC1 pre_q6_polparty_REC2 pre_q14_c_confession_REC1b index_connect_wgt4 pre_q11_a_zaimhelp_REC1 pre_q11_b_relhelp_REC1 pre_q14_a_agegrp_REC1b pre_q14_b_gender_REC1b pre_q14_d_class_REC1b pre_q14_leb_REC1b pre_q14_f_occupation_REC1b $c_implement $pgg_payoffs_all

	
******************************************************************************************************************************************************
* FIGURE 1: DISTRIBUTION OF CONTRIBUTIONS TO THE GROUP POT BY CLASS TREATMENT AND SES, WOMEN ONLY
******************************************************************************************************************************************************
	
*Install the misum program if necessary	
		ssc install misum
	
*Distribution of Round 1 contributions by treatment and econ class, women only		
	
		misum pgg_rd1 [fw=wgt4_fig] if female==1 & rich==0 & T_mixed_class==0		
			* female, poor same class mean = 3830
		misum pgg_rd1 [fw=wgt4_fig] if female==1 & rich==0 & T_mixed_class==1		
			* female, poor mixed class mean = 3110			
		misum pgg_rd1 [fw=wgt4_fig] if female==1 & rich==1 & T_mixed_class==0		
			* female, rich same class mean = 4476
		misum pgg_rd1 [fw=wgt4_fig] if female==1 & rich==1 & T_mixed_class==1		
			* female, rich mixed class mean = 2442
			
		histogram pgg_rd1 [fw=wgt4_fig] if female==1 & rich==0 & T_mixed_class==0, scheme(lean1) gap(10) percent discrete addl addlabopts(yvarformat(%2.0f)) ///
		xlab(0(1000)10000, labsize(small) angle(15)) xline(3830, lp(dash_dot) lwidth(thick)) text(30 5200 "Mean =" "3,830 LBP") ylab(0(10)45, nogrid) ///
		title("Poor Women in Same Class Groups") xtitle("Out of 10,000 LBP") ytitle("Respondents (%)")
			graph save "$results_figs/pgg_rd1_sameclass_poor_female.gph", replace

		histogram pgg_rd1 [fw=wgt4_fig] if female==1 & rich==0 & T_mixed_class==1, scheme(lean1) gap(10) percent discrete addl addlabopts(yvarformat(%2.0f)) ///
		xlab(0(1000)10000, labsize(small) angle(15)) xline(3110, lp(dash_dot) lwidth(thick)) text(30 4500 "Mean =" "3,110 LBP") ylab(0(10)45, nogrid) ///
		title("Poor Women in Mixed Class Groups") xtitle("Out of 10,000 LBP") ytitle("Respondents (%)")
			graph save "$results_figs/pgg_rd1_mixedclass_poor_female.gph", replace

		histogram pgg_rd1 [fw=wgt4_fig] if female==1 & rich==1 & T_mixed_class==0, scheme(lean1) gap(10) percent discrete addl addlabopts(yvarformat(%2.0f)) ///
		xlab(0(1000)10000, labsize(small) angle(15)) xline(4476, lp(dash_dot) lwidth(thick)) text(30 5700 "Mean =" "4,476 LBP") ylab(0(10)45, nogrid) ///
		title("Rich Women in Same Class Groups") xtitle("Out of 10,000 LBP") ytitle("Respondents (%)")
			graph save "$results_figs/pgg_rd1_sameclass_rich_female.gph", replace

		histogram pgg_rd1 [fw=wgt4_fig] if female==1 & rich==1 & T_mixed_class==1, scheme(lean1) gap(10) percent discrete addl addlabopts(yvarformat(%2.0f)) ///
		xlab(0(1000)10000, labsize(small) angle(15)) xline(2442, lp(dash_dot) lwidth(thick)) text(30 3700 "Mean =" "2,442 LBP") ylab(0(10)45, nogrid) ///
		title("Rich Women in Mixed Class Groups") xtitle("Out of 10,000 LBP") ytitle("Respondents (%)")
			graph save "$results_figs/pgg_rd1_mixedclass_rich_female.gph", replace

#delimit ;
	graph combine
		"$results_figs/pgg_rd1_sameclass_poor_female.gph"
		"$results_figs/pgg_rd1_mixedclass_poor_female.gph"
		"$results_figs/pgg_rd1_sameclass_rich_female.gph"
		"$results_figs/pgg_rd1_mixedclass_rich_female.gph",		
		cols(2) rows(2) xcommon ycommon plotregion(fcolor(white)) graphregion(fcolor(white)) 
		title("Contributions to the Public Good") ;
#delimit cr		
	graph save "$results_figs/pgg_rd1_women_byclasstreat.gph", replace	
	graph export "$results_figs/pgg_rd1_women_byclasstreat.pdf", replace	

	
	
*******************************************************
* TABLE 2
*******************************************************

*Results for women
	est clear
	
	eststo: mi estimate, post: mean pgg_rd1 [pweight=wgt4] if female==1, over(T_mixed_class)
	
	eststo: mi estimate, post: mean pgg_rd1 [pweight=wgt4] if female==1, over(T_mixed_class rich)
	
	estout using "$results_tables/pgg_rd1_female.xls", replace cells(b(fmt(0))) title(Group Means)

	est clear
		eststo: mi estimate, post: 		       reg   pgg_rd1  T_mixed_class					                     [pweight=wgt4]	if female==1, vce(robust)	
		eststo: mi estimate, post: 		       reg   pgg_rd1  T_mixed_class          $c_main $c_implement        [pweight=wgt4]	if female==1, vce(robust)	

		eststo: mi estimate, post: 		       reg   pgg_rd1  T_mixed_class					                     [pweight=wgt4]	if female==1 & rich==0, vce(robust)	
		eststo: mi estimate, post: 			   reg   pgg_rd1  T_mixed_class			 $c_main_noecon $c_implement [pweight=wgt4]	if female==1 & rich==0, vce(robust)		

		eststo: mi estimate, post: 			   reg   pgg_rd1  T_mixed_class					                     [pweight=wgt4]	if female==1 & rich==1, vce(robust)	
		eststo: mi estimate, post: 			   reg   pgg_rd1  T_mixed_class			 $c_main_noecon $c_implement [pweight=wgt4]	if female==1 & rich==1, vce(robust)	

	estout using "$results_tables/pgg_rd1_female.xls", la append cells(b(fmt(%9.0f)) se(par(`"="("'`")""') fmt(%9.0f)) p(fmt(%9.3f))) keep(T_mixed_class* _cons)  stats(N)  mlabels(`outcome') nobaselevels noomitted

*Results for men
	est clear
	
	eststo: mi estimate, post: mean pgg_rd1 [pweight=wgt4] if male==1, over(T_mixed_class)
	
	eststo: mi estimate, post: mean pgg_rd1 [pweight=wgt4] if male==1, over(T_mixed_class rich)
	
	estout using "$results_tables/pgg_rd1_male.xls", replace cells(b(fmt(0))) title(Group Means)

	est clear
		eststo: mi estimate, post: 		       reg   pgg_rd1  T_mixed_class					                     [pweight=wgt4]	if male==1, vce(robust)	
		eststo: mi estimate, post: 		       reg   pgg_rd1  T_mixed_class          $c_main $c_implement        [pweight=wgt4]	if male==1, vce(robust)	

		eststo: mi estimate, post: 		       reg   pgg_rd1  T_mixed_class					                     [pweight=wgt4]	if male==1 & rich==0, vce(robust)	
		eststo: mi estimate, post: 			   reg   pgg_rd1  T_mixed_class			 $c_main_noecon $c_implement [pweight=wgt4]	if male==1 & rich==0, vce(robust)		

		eststo: mi estimate, post: 			   reg   pgg_rd1  T_mixed_class					                     [pweight=wgt4]	if male==1 & rich==1, vce(robust)	
		eststo: mi estimate, post: 			   reg   pgg_rd1  T_mixed_class			 $c_main_noecon $c_implement [pweight=wgt4]	if male==1 & rich==1, vce(robust)	

	estout using "$results_tables/pgg_rd1_male.xls", la append cells(b(fmt(%9.0f)) se(par(`"="("'`")""') fmt(%9.0f)) p(fmt(%9.3f))) keep(T_mixed_class*  _cons)  stats(N)  mlabels(`outcome') nobaselevels noomitted

*******************************************************
* TABLE 3: SECTARIAN DIFFERENCES
*******************************************************
	
	est clear
	
	eststo: mi estimate, post: mean pgg_rd1 [pweight=wgt4] if female==1, over(T_mixed_sect)
	eststo: mi estimate, post: mean pgg_rd1 [pweight=wgt4] if male==1, over(T_mixed_sect)
		
	estout using "$results_tables/pgg_rd1_sect.xls", replace cells(b(fmt(0))) title(Group Means)

	est clear
		eststo: mi estimate, post:  reg   pgg_rd1  T_mixed_sect					                     [pweight=wgt4]	if female==1, vce(robust)	
		eststo: mi estimate, post:  reg   pgg_rd1  T_mixed_sect          $c_main $c_implement_nosect [pweight=wgt4]	if female==1, vce(robust)	

		eststo: mi estimate, post:  reg   pgg_rd1  T_mixed_sect					                     [pweight=wgt4]	if male==1, vce(robust)	
		eststo: mi estimate, post:  reg   pgg_rd1  T_mixed_sect          $c_main $c_implement_nosect [pweight=wgt4]	if male==1, vce(robust)	

		
	estout using "$results_tables/pgg_rd1_sect.xls", la append cells(b(fmt(%9.0f)) se(par(`"="("'`")""') fmt(%9.0f)) p(fmt(%9.3f))) keep(T_mixed_sect _cons)  stats(N)  mlabels(`outcome') nobaselevels noomitted


*************************************************
*TABLE 4: CLASS VERSUS SECTARIAN DIFFERENCES
*************************************************

*Program for marginal effects
	capture program drop leb_pgg_margins
	program leb_pgg_margins, eclass properties(mi)
		syntax varlist [if]
		marksample touse, novarlist
		preserve
		keep if `touse'
		args DV
		version 14
			est clear
			reg `DV' T_mixed_sect##T_mixed_class [pweight=wgt4], vce(robust)
			margins, dydx(T_mixed_sect T_mixed_class) at(T_mixed_class=(0 1) T_mixed_sect=(0 1)) atmeans post
			eststo margins
	restore
	end

*Results for women
		est clear
		eststo: mi estimate, post: mean pgg_rd1 [pweight=wgt4] if female==1	, over(T_mixed_sect T_mixed_class)
		estout using "$results_tables/leb_pgg_2x2_female'.xls", cells(b(fmt(3))) replace title(Group Means)

		est clear
		eststo: mi estimate, post: reg pgg_rd1 T_mixed_sect##T_mixed_class [pweight=wgt4] if female==1,  vce(robust)  
		estout using "$results_tables/leb_pgg_2x2_female.xls", la append cells(b(fmt(%9.0f)) se(par(`"="("'`")""') fmt(%9.0f)) p(fmt(%9.3f))) stardrop(_cons) stats(N) noomitted nobaselevels mlabels(`outcome') 
	
		est clear
		eststo: mi estimate, cmdok post: leb_pgg_margins pgg_rd1 if female==1
		estout using "$results_tables/leb_pgg_2x2_female.xls", la append cells(b(fmt(%9.0f)) se(par(`"="("'`")""') fmt(%9.0f)) p(fmt(%9.3f))) noomitted nobaselevels 

*Results for men
		est clear
		eststo: mi estimate, post: mean pgg_rd1 [pweight=wgt4] if male==1	, over(T_mixed_sect T_mixed_class)
		estout using "$results_tables/leb_pgg_2x2_male'.xls", cells(b(fmt(3))) replace title(Group Means)

		est clear
		eststo: mi estimate, post: reg pgg_rd1 T_mixed_sect##T_mixed_class [pweight=wgt4] if male==1,  vce(robust)  
		estout using "$results_tables/leb_pgg_2x2_male.xls", la append cells(b(fmt(%9.0f)) se(par(`"="("'`")""') fmt(%9.0f)) p(fmt(%9.3f))) stardrop(_cons) stats(N) noomitted nobaselevels mlabels(`outcome') 
	
		est clear
		eststo: mi estimate, cmdok post: leb_pgg_margins pgg_rd1 if male==1
		estout using "$results_tables/leb_pgg_2x2_male.xls", la append cells(b(fmt(%9.0f)) se(par(`"="("'`")""') fmt(%9.0f)) p(fmt(%9.3f))) noomitted nobaselevels 
	
		
********************************************************************************************	
*APPENDIX	
********************************************************************************************	
	
*************************************************
*TABLE B.1: BALANCE
*************************************************

*Balance for women (if you have already produced this table, erase the older version first)
	*erase "$results_tables/pgg_balance_female.xls"			
	
	foreach var of varlist $c_all {
		est clear
		eststo: mi estimate, post: reg `var' T_mixed_class [pweight=wgt4] if female==1, vce(robust)
		estout using "$results_tables/pgg_balance_female.xls", la append cells((b(fmt(%9.2f)) p(fmt(%9.3f)))) drop(_cons)  mlabel(`var') 
	}

	
*Balance for men (if you have already produced this table, erase the older version first)	
	*erase "$results_tables/pgg_balance_male.xls"			
	
	foreach var of varlist $c_all {
		est clear
		eststo: mi estimate, post: reg `var' T_mixed_class [pweight=wgt4] if male==1, vce(robust)
		estout using "$results_tables/pgg_balance_male.xls", la append cells((b(fmt(%9.2f)) p(fmt(%9.3f)))) drop(_cons)  mlabel(`var') 
	}
	
	
*************************************************
*TABLE B.2: BALANCE (FACTORIAL, WOMEN)
*Cannot run balance on post_q7_knowppl_REC1 for some reason -- use diff macro
*************************************************

	global c_almost_all pre_q2_age married scr_q4_educ_REC2 pre_q3_christian pre_q3_shia pre_q3_sunni index_c_econ_all_wgt4 scr_q9_assets_REC1 scr_q10_hh_area scr_q11_summer_hh scr_q12_poweralt scr_q13_vacay scr_q14_dineout_REC1 scr_q15_income scr_q16_income_subj_REC1 pre_q8_income_REC1 pre_q9_class_REC3b student homemaker index_c_prej_wgt4 pre_q12_a_marry_REC1 pre_q12_b_doctor_REC1 pre_q12_c_neighbor_REC1 pre_q12_d_poltalk_REC1 pre_q12_e_econtalk_REC1 pre_q12_f_supervisor_REC1 pre_q12_g_friends_REC1 index_c_act_wgt4 pre_q7_a_friends_REC1 pre_q7_b_elites_REC1 pre_q7_c_petition_REC1 pre_q7_d_protest_REC1 index_c_soc_hom_wgt4 pre_q10_diffecon_REC1 pre_q5_diffsect_REC1 pre_q13_disagree_REC1 index_c_sect_id_wgt4 pre_q4_changesect_REC1 pre_q6_polparty_REC2 pre_q14_c_confession_REC1b index_connect_wgt4 pre_q11_a_zaimhelp_REC1 pre_q11_b_relhelp_REC1 pre_q14_a_agegrp_REC1b pre_q14_b_gender_REC1b pre_q14_d_class_REC1b pre_q14_leb_REC1b pre_q14_f_occupation_REC1b mod1 n_gid days_to_elex pgg_prac_prob
	
*If you have already produced this table, erase the older version first	
	*erase "$results_tables/pgg_balance_xcut_fem.xls"			
	
	foreach var of varlist $c_almost_all {
			est clear
			eststo: mi estimate, post: mean `var' [pweight=wgt4] if T_mixed_sect==0 & T_mixed_class==0 & female==1
			estout using "$results_tables/pgg_balance_xcut_fem.xls", cells(b(fmt(3))) append title(Group Means)

			est clear
			eststo: mi estimate, post: reg `var' T_mixed_sect##T_mixed_class [pweight=wgt4] if female==1, vce(robust)
			estout using "$results_tables/pgg_balance_xcut_fem.xls", la append cells((b(fmt(%9.2f)) p(fmt(%9.3f)))) drop(_cons)  mlabel(`var') noomitted nobaselevels 
	}

*************************************************
*TABLE B.3: BALANCE (FACTORIAL, MEN)
*************************************************

*If you have already produced this table, erase the older version first
	*erase "$results_tables/pgg_balance_xcut_male.xls"			
	
	foreach var of varlist $c_all {
			est clear
			eststo: mi estimate, post: mean `var' [pweight=wgt4] if T_mixed_sect==0 & T_mixed_class==0 & male==1
			estout using "$results_tables/pgg_balance_xcut_male.xls", cells(b(fmt(3))) append title(Group Means)

			est clear
			eststo: mi estimate, post: reg `var' T_mixed_sect##T_mixed_class [pweight=wgt4] if male==1, vce(robust)
			estout using "$results_tables/pgg_balance_xcut_male.xls", la append cells((b(fmt(%9.2f)) p(fmt(%9.3f)))) drop(_cons)  mlabel(`var') noomitted nobaselevels 
	}
	
	

*************************************************
*APPENDIX C: COMPARISON OF SAMPLE TO POPULATION
*RESULTS FOR SAMPLE
*************************************************
	
*TABLES C.1 and C.2: SAMPLE
	global disc_vars1 scr_q1_gender_REC2 pre_q2_age scr_q3_marital_REC2 scr_q4_educ_REC2 pre_q3_christian pre_q3_sunni pre_q3_shia 
	global disc_vars2 pre_q8_income_REC1 employed pre_q9_class_REC1b scr_q16_income_subj_REC1 concern_unemp_all concern_prices_all   
	global disc_vars3 pre_q11_a_zaimhelp_REC1 pre_q11_b_relhelp_REC1
	global disc_vars4 pre_q7_b_elites_REC1 pre_q7_c_petition_REC1 pre_q7_d_protest_REC1 
	global disc_vars5 pre_q12_a_marry_REC1 pre_q12_c_neighbor_REC1 pre_q12_f_supervisor_REC1 pre_q12_g_friends_REC1 
	global disc_vars6 pre_q5_diffsect_REC1 pre_q10_diffecon_REC1 pre_q13_disagree_REC1
	
	
	est clear
		estpost tabstat $disc_vars1 $disc_vars2 $disc_vars3 $disc_vars4 $disc_vars5 $disc_vars6  if m_impute==1 & female==1, statistics(mean sd min max count) columns(statistics)
		esttab using "$results_tables/samp_women.csv", cells("mean sd min max count") nomtitle nonumber noobs replace l

	est clear
		estpost tabstat $disc_vars1 $disc_vars2 $disc_vars3 $disc_vars4 $disc_vars5 $disc_vars6  if m_impute==1 & male==1, statistics(mean sd min max count) columns(statistics)
		esttab using "$results_tables/samp_men.csv", cells("mean sd min max count") nomtitle nonumber noobs replace l


/*************************************************
*APPENDIX C: COMPARISON OF SAMPLE TO POPULATION
*RESULTS FOR POPULATION
*Note: To run the below you need to call the dataset "Leb_national_survey" and run all code in this
section on that dataset. You then need to restore the experiment dataset at the end to continue
with the analysis.
***************************************************/

preserve
	use "$data_orig/Leb_national_survey.dta", clear

	g beirut_mtleb = mohafaza=="Beirut" | mohafaza=="Mount Lebanon" 
	
	global surv_vars1 female age q30_married q31_educ_REC2 q33_christian q33_sunni q33_shia q35_sect_fut_REC1
	global surv_vars2 q69_hh_income_REC1 employed q71_econ_class_REC1b q70_subj_income_REC1 concern_unemp_all concern_prices_all
	global surv_vars3 q36_a_help_zaim_REC1 q36_b_help_rel_REC1
	global surv_vars4 q41_b_zaim q41_c_petition q41_d_protest
	global surv_vars5 q59_e_marry_REC1 q59_a_neighbors_REC1 q59_b_supervised_REC1 q59_c_friends_REC1 
	global surv_vars6 q43_sect_contact_REC1 q44_econ_contact_REC1 q45_debate
	
	
*Set the survey weight (options are wgt_main2a wgt_post2a wgt_ebal2)  	
	mi svyset id_psu [pweight=wgt_ebal2b]
	

*TABLE C.1: Women
*WOMEN From Beirut/Mount Lebanon
	est clear
	scalar drop _all
	return clear
	ereturn clear
	foreach y of varlist $surv_vars1 $surv_vars2 $surv_vars3 $surv_vars4 $surv_vars5 $surv_vars6 {
		cap drop y2
		qui: mi xeq : generate y2 = `y'^2 
		eststo: mi est, esampvaryok post: svy: mean `y' y2 if female==1 & beirut_mtleb==1
		estadd scalar meanUp = _b[`y']
		estadd scalar sdUp = sqrt( _b[y2] - _b[`y']^2 )
		estadd scalar nUp = (e(N))
		cap drop y2
	}
		
	esttab using "$results_tables/pop_women_beirut.csv", l replace scalar(meanUp sdUp nUp) sfmt(%9.2f %9.2f %9.0f) cells(none) noobs equations(1) mti($surv_vars1 $surv_vars2 $surv_vars3 $surv_vars4 $surv_vars5 $surv_vars6) wide 

*WOMEN From whole country
	est clear
	scalar drop _all
	return clear
	ereturn clear
	foreach y of varlist $surv_vars1 $surv_vars2 $surv_vars3 $surv_vars4 $surv_vars5 $surv_vars6 {
		cap drop y2
		qui: mi xeq : generate y2 = `y'^2 
		eststo: mi est, esampvaryok post: svy: mean `y' y2 if female==1
		estadd scalar meanUp = _b[`y']
		estadd scalar sdUp = sqrt( _b[y2] - _b[`y']^2 )
		estadd scalar nUp = (e(N))
		cap drop y2
	}
		
	esttab using "$results_tables/pop_women_leb.csv", l replace scalar(meanUp sdUp nUp) sfmt(%9.2f %9.2f %9.0f) cells(none) noobs equations(1) mti($surv_vars1 $surv_vars2 $surv_vars3 $surv_vars4 $surv_vars5 $surv_vars6) wide

*TABLE C.2: MEN
*MEN From Beirut/Mount Lebanon
	est clear
	scalar drop _all
	return clear
	ereturn clear
	foreach y of varlist $surv_vars1 $surv_vars2 $surv_vars3 $surv_vars4 $surv_vars5 $surv_vars6 {
		cap drop y2
		qui: mi xeq : generate y2 = `y'^2 
		eststo: mi est, esampvaryok post: svy: mean `y' y2 if female==0 & beirut_mtleb==1
		estadd scalar meanUp = _b[`y']
		estadd scalar sdUp = sqrt( _b[y2] - _b[`y']^2 )
		estadd scalar nUp = (e(N))
		cap drop y2
	}
		
	esttab using "$results_tables/pop_men_beirut.csv", l replace scalar(meanUp sdUp nUp) sfmt(%9.2f %9.2f %9.0f) cells(none) noobs equations(1) mti($surv_vars1 $surv_vars2 $surv_vars3 $surv_vars4 $surv_vars5 $surv_vars6) wide 

*MEN From whole country 
	est clear
	scalar drop _all
	return clear
	ereturn clear
	foreach y of varlist $surv_vars1 $surv_vars2 $surv_vars3 $surv_vars4 $surv_vars5 $surv_vars6 {
		cap drop y2
		qui: mi xeq : generate y2 = `y'^2 
		eststo: mi est, esampvaryok post: svy: mean `y' y2 if female==0
		estadd scalar meanUp = _b[`y']
		estadd scalar sdUp = sqrt( _b[y2] - _b[`y']^2 )
		estadd scalar nUp = (e(N))
		cap drop y2
	}
		
	esttab using "$results_tables/pop_men_leb.csv", l replace scalar(meanUp sdUp nUp) sfmt(%9.2f %9.2f %9.0f) cells(none) noobs equations(1) mti($surv_vars1 $surv_vars2 $surv_vars3 $surv_vars4 $surv_vars5 $surv_vars6) wide


*IMPORTANT: Use this to restore the other dataset (with all macros, etc) before continuing!	
restore
	
	
*************************************************
*TABLE E.1: SUMMARY STATS
*Make sure that the original dataset is restored (with all the global macros already run)
*************************************************

	mi svyset gid [pweight=wgt1], strata(strata_agg_FINAL2) singleunit(centered) 

*SUMMARY STATS FOR WOMEN	
*Outcomes	
	est clear
	scalar drop _all
	return clear
	ereturn clear
	foreach y of varlist $c_all {
		cap drop y2
		qui: mi xeq : generate y2 = `y'^2 
		eststo: mi est, esampvaryok post: svy: mean `y' y2 if female==1
		estadd scalar meanUp = _b[`y']
		estadd scalar sdUp = sqrt( _b[y2] - _b[`y']^2 )
		estadd scalar nUp = (e(N))
		cap drop y2
	}
		
	esttab using "$results_tables/pgg_sumstats_women1.csv", l replace scalar(meanUp sdUp nUp) sfmt(%9.2f %9.2f %9.0f) cells(none) noobs equations(1) mti($c_all) wide

	
*Get variable ranges and percentage missingness imputed	
	est clear
		estpost tabstat $c_all if m_impute==1 & female==1, statistics(mean sd min max count) columns(statistics)
		esttab using "$results_tables/pgg_sumstats_women2.csv", cells("mean sd min max count") nomtitle nonumber noobs replace 


*SUMMARY STATS FOR MEN
	est clear
	scalar drop _all
	return clear
	ereturn clear
	foreach y of varlist $c_all {
		cap drop y2
		qui: mi xeq : generate y2 = `y'^2 
		eststo: mi est, esampvaryok post: svy: mean `y' y2 if female==0
		estadd scalar meanUp = _b[`y']
		estadd scalar sdUp = sqrt( _b[y2] - _b[`y']^2 )
		estadd scalar nUp = (e(N))
		cap drop y2
	}
		
	esttab using "$results_tables/pgg_sumstats_men1.csv", l replace scalar(meanUp sdUp nUp) sfmt(%9.2f %9.2f %9.0f) cells(none) noobs equations(1) mti($c_all) wide

	
*Get variable ranges and percentage missingness imputed	
	est clear
		estpost tabstat $c_all if m_impute==1 & female==0, statistics(mean sd min max count) columns(statistics)
		esttab using "$results_tables/pgg_sumstats_men2.csv", cells("mean sd min max count") nomtitle nonumber noobs replace 

	
*************************************************
*TABLE F.1: ROBUSTNESS
*************************************************

capture program drop pgg_robust
	program pgg_robust
	version 14.0	
	syntax varlist [if]
	marksample touse, novarlist
	gettoken het : varlist
	preserve
	keep if `touse'
	
	local sex `het'
	
	est clear
	
		eststo: mi estimate, esampvaryok post: mean pgg_rd1 [pweight=wgt1] if `sex'==1	, over(T_mixed_class)
		eststo: mi estimate, esampvaryok post: mean pgg_rd1 [pweight=wgt4] if `sex'==1	, over(T_mixed_class)
		eststo: mi estimate, esampvaryok post: mean pgg_rd1 [pweight=wgt6] if `sex'==1	, over(T_mixed_class)
					
		estout using "$results_tables/leb_pgg_robust_`sex'.xls", cells(b(fmt(3))) replace title(Group Means)
	

	est clear
		
		eststo: mi estimate, post: 	reg   pgg_rd1  T_mixed_class					             [pweight=wgt1]	if `sex'==1, vce(robust)	
		eststo: mi estimate, post:  reg   pgg_rd1  T_mixed_class     $c_main $c_implement        [pweight=wgt1]	if `sex'==1, vce(robust)	

		eststo: mi estimate, post: 	reg   pgg_rd1  T_mixed_class					             [pweight=wgt4]	if `sex'==1, vce(robust)	
		eststo: mi estimate, post:  reg   pgg_rd1  T_mixed_class     $c_main $c_implement        [pweight=wgt4]	if `sex'==1, vce(robust)	
		
		eststo: mi estimate, post: areg   pgg_rd1  T_mixed_class					                            if `sex'==1, vce(robust) absorb(strata_agg_FINAL2) 	
		eststo: mi estimate, post: areg   pgg_rd1  T_mixed_class	 $c_main $c_implement 						if `sex'==1, vce(robust) absorb(strata_agg_FINAL2) 		

		eststo: mi estimate, post: 	reg   pgg_rd1  T_mixed_class					             [pweight=wgt6]	if `sex'==1, vce(robust)	
		eststo: mi estimate, post: 	reg   pgg_rd1  T_mixed_class     $c_main $c_implement        [pweight=wgt6]	if `sex'==1, vce(robust)	
		

	estout using "$results_tables/leb_pgg_robust_`sex'.xls", la append cells(b(fmt(%9.0f)) se(par(`"="("'`")""') fmt(%9.0f)) p(fmt(%9.3f))) keep(T_mixed_class* )  stats(N)  mlabels(`outcome') nobaselevels noomitted

	restore
	end

	
*RUN PROGRAM
	pgg_robust female
	
	pgg_robust male
	
	
*****************************************************************************
*TABLE G.1: Contributions by Class and Sectarian Composition (With Controls)
*****************************************************************************
	
*Run the program first
	capture program drop leb_pgg_margins_controls
	program leb_pgg_margins_controls, eclass properties(mi)
		syntax varlist [if]
		marksample touse, novarlist
		preserve
		keep if `touse'
		args DV
		version 14
			est clear
			reg `DV' T_mixed_sect##T_mixed_class $c_main $c_implement [pweight=wgt4] , vce(robust)
			margins, dydx(T_mixed_sect T_mixed_class) at(T_mixed_class=(0 1) T_mixed_sect=(0 1)) atmeans post
			eststo margins
	restore
	end

*Results for women
		est clear
		eststo: mi estimate, post: reg pgg_rd1 T_mixed_sect##T_mixed_class $c_main $c_implement [pweight=wgt4] if female==1,  vce(robust)  
		estout using "$results_tables/leb_pgg_xcut_fem_controls.xls", la replace cells(b(fmt(%9.0f)) se(par(`"="("'`")""') fmt(%9.0f)) p(fmt(%9.3f))) stardrop(_cons) stats(N) noomitted nobaselevels mlabels(`outcome') drop($c_main $c_implement)
	
		est clear
		eststo: mi estimate, cmdok post: leb_pgg_margins_controls pgg_rd1 if female==1
		estout using "$results_tables/leb_pgg_xcut_fem_controls.xls", la append cells(b(fmt(%9.0f)) se(par(`"="("'`")""') fmt(%9.0f)) p(fmt(%9.3f))) noomitted nobaselevels 


*Results for men
		est clear
		eststo: mi estimate, post: reg pgg_rd1 T_mixed_sect##T_mixed_class $c_main $c_implement [pweight=wgt4] if male==1,  vce(robust)  
		estout using "$results_tables/leb_pgg_xcut_male.xls", la replace cells(b(fmt(%9.0f)) se(par(`"="("'`")""') fmt(%9.0f)) p(fmt(%9.3f))) stardrop(_cons) stats(N) noomitted nobaselevels mlabels(`outcome') drop($c_main $c_implement)
	
		est clear
		eststo: mi estimate, cmdok post: leb_pgg_margins_controls pgg_rd1 if male==1
		estout using "$results_tables/leb_pgg_xcut_male.xls", la append cells(b(fmt(%9.0f)) se(par(`"="("'`")""') fmt(%9.0f)) p(fmt(%9.3f))) noomitted nobaselevels 
	
*****************************************************************************
*TABLE G.2: *RESULTS DISAGGREGATED BY CLASS	(NO CONTROLS)
*****************************************************************************
*Repeat for poor and rich women	
	foreach var of varlist poor rich {
		est clear
		eststo: mi estimate, post: mean pgg_rd1 [pweight=wgt4] if `var'==1 & female==1, over(T_mixed_sect T_mixed_class)
		estout using "$results_tables/leb_pgg_2x2_fem_`var'.xls", cells(b(fmt(3))) replace title(Group Means)

		est clear
		eststo: mi estimate, post: reg pgg_rd1 T_mixed_sect##T_mixed_class [pweight=wgt4] if `var'==1 & female==1,  vce(robust)  
		estout using "$results_tables/leb_pgg_2x2_fem_`var'.xls", la append cells(b(fmt(%9.0f) star) se(par(`"="("'`")""') fmt(%9.0f)) p(fmt(%9.3f))) stardrop(_cons) stats(N) noomitted nobaselevels mlabels(`outcome') starl(* .10 ** .05  *** .01)
	
		est clear
		eststo: mi estimate, cmdok post: leb_pgg_margins pgg_rd1 if `var'==1 & female==1
		estout using "$results_tables/leb_pgg_2x2_fem_`var'.xls", la append cells(b(fmt(%9.0f) star) se(par(`"="("'`")""') fmt(%9.0f)) p(fmt(%9.3f))) noomitted nobaselevels starl(* .10 ** .05  *** .01)
	}


*Repeat for poor and rich men	
	foreach var of varlist poor rich {
		est clear
		eststo: mi estimate, post: mean pgg_rd1 [pweight=wgt4] if `var'==1 & male==1, over(T_mixed_sect T_mixed_class)
		estout using "$results_tables/leb_pgg_2x2_men_`var'.xls", cells(b(fmt(3))) replace title(Group Means)

		est clear
		eststo: mi estimate, post: reg pgg_rd1 T_mixed_sect##T_mixed_class [pweight=wgt4] if `var'==1 & male==1,  vce(robust)  
		estout using "$results_tables/leb_pgg_2x2_men_`var'.xls", la append cells(b(fmt(%9.0f) star) se(par(`"="("'`")""') fmt(%9.0f)) p(fmt(%9.3f))) stardrop(_cons) stats(N) noomitted nobaselevels mlabels(`outcome') starl(* .10 ** .05  *** .01)
	
		est clear
		eststo: mi estimate, cmdok post: leb_pgg_margins pgg_rd1 if `var'==1 & male==1
		estout using "$results_tables/leb_pgg_2x2_men_`var'.xls", la append cells(b(fmt(%9.0f) star) se(par(`"="("'`")""') fmt(%9.0f)) p(fmt(%9.3f))) noomitted nobaselevels starl(* .10 ** .05  *** .01)
	}
	

	
*************************************************
*TABLE H.1: TESTING UNCERTAINTY/BIAS MECHANISM -- HETEROGENEOUS EFFECTS BY SOCIAL NETWORK TYPE
*************************************************

	mi estimate: reg pre_q10_diffecon_REC1 female [pweight=wgt4], vce(robust)	
	
	mi estimate: mean pre_q10_diffecon_REC3_DUM3 if female==1

	mi estimate: mean pre_q10_diffecon_REC3_DUM3 if male==1


*Run program first	
	capture program drop leb_pgg_soc
		program leb_pgg_soc, eclass properties(mi)
		args sex ses
		version 14
			reg pgg_rd1 T_mixed_class##pre_q10_diffecon_REC3 `newlist' [pweight=wgt4] if `sex'==1 & `ses'==1, vce(robust)
			margins, dydx(T_mixed_class) at(pre_q10_diffecon_REC3=(0 1 2)) atmeans post
			eststo margins
	end

	
*Prepare controls macro (drop the control index that includes the het var)
	unab varlist  : $c_main
	unab drop_var : index_c_soc_hom_wgt4
	local newlist : list varlist - drop_var


*Produces Results for All Women and Men
	foreach grp in male female {
		foreach subgrp in all poor rich {
			est clear

			eststo: mi estimate, post: mean pgg_rd1 [pweight=wgt4] if `grp'==1 & `subgrp'==1, over(T_mixed_class pre_q10_diffecon_REC3)
			estout using "$results_tables/leb_pgg_soc_`grp'_`subgrp'.xls", replace cells(b(fmt(0))) title(Group Means)

			est clear
	
			eststo: mi estimate, cmdok post: leb_pgg_soc `grp' `subgrp'

			estout using "$results_tables/leb_pgg_soc_`grp'_`subgrp'.xls", append cells(b(fmt(%9.0f) star) se(par(`"="("'`")""') fmt(%9.0f)) p(fmt(%9.3f))) starl(* .10 ** .05  *** .01) noomitted nobaselevels 
	}
	}
	
*************************************************
*TABLE H.2: STRENGTH OF GENDER ID AND POLICY PREFERENCES
*************************************************

*Strength of gender ID by gender and class
	est clear

	eststo: mi estimate, post: mean pre_q14_b_gender_REC2 [pweight=wgt4], over(female)
	eststo: mi estimate, post: mean pre_q14_b_gender_REC2 [pweight=wgt4], over(female poor)

	eststo: mi estimate, post: mean pre_q14_gender_top3 [pweight=wgt4], over(female)
	eststo: mi estimate, post: mean pre_q14_gender_top3 [pweight=wgt4], over(female poor)

	estout using "$results_tables/pgg_genderid1.xls", replace cells(b(fmt(%9.2f)) se(par(`"="("'`")""') fmt(%9.2f))) title(Group Means)

*Regression version	
	est clear
	
	unab varlist  : $c_main_noecon
	unab drop_var : pre_q14_b_gender_REC1b
	local newlist : list varlist - drop_var

	eststo: mi estimate, post: reg pre_q14_b_gender_REC2 rich `newlist' T_mixed_class T_mixed_sect [pweight=wgt4] if female==1, vce(robust)
	eststo: mi estimate, post: reg pre_q14_b_gender_REC2 rich `newlist' T_mixed_class T_mixed_sect [pweight=wgt4] if male==1, vce(robust)
	
	estout using "$results_tables/pgg_genderid2.xls", replace cells(b(fmt(%9.2f) star) se(par(`"="("'`")""') fmt(%9.2f))) starl(* .10 ** .05  *** .01) noomitted nobaselevels 

	
*Testing effect of class on support for civil marriage	
	est clear

	eststo: mi estimate, post: reg post_q12_marriage_REC2b rich $c_main_noecon T_mixed_class T_mixed_sect [pweight=wgt4] if female==1, vce(robust)
	
	eststo: mi estimate, post: reg post_q12_marriage_REC2b rich $c_main_noecon T_mixed_class T_mixed_sect [pweight=wgt4] if male==1, vce(robust)
	
	estout using "$results_tables/leb_pgg_civmar.xls", replace cells(b(fmt(%9.2f) star) se(par(`"="("'`")""') fmt(%9.2f))) starl(* .10 ** .05  *** .01) noomitted nobaselevels 

	log close
	
	
	
*END	
	
	
